/* 
 * AUTHOR: Kevin Lam
 */

package com.apps.ubc.cc.ajax;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apps.datastore.UBCCourseSpiderDatastore;
import com.apps.datastore.dao.CourseInformationObject;
import com.apps.datastore.dao.DepartmentInformationObject;
import com.apps.datastore.dao.SectionInformationObject;
import com.apps.utils.CourseSearchUtils;

public class SearchSuggestionController extends HttpServlet {
	
	private static UBCCourseSpiderDatastore d = new UBCCourseSpiderDatastore();
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) {
		String searchQuery = req.getParameter("searchQuery");
		String[] searchTerms = null;
		String dept = "";
		String course = "";
		String response = "";
		String prefix = "";
		searchQuery = CourseSearchUtils.parseSearch(searchQuery);
		if(searchQuery.contains(" "))
			searchTerms = searchQuery.split(" ");
		else {
			searchTerms = new String[1];
			searchTerms[0] = searchQuery;
		}	
		if (searchTerms != null) {
			DepartmentInformationObject firstDept = null;
				if (searchTerms.length == 1) {
					prefix = searchTerms[0];
					List<DepartmentInformationObject> diol = d.queryDepartmentByPrefix(prefix);
					if(!diol.isEmpty()) {
						for(Iterator<DepartmentInformationObject> i = diol.iterator(); i.hasNext();) {
							DepartmentInformationObject dio = i.next();
							String deptId = dio.getDepartmentId();
							String title = "<i>"+dio.getSubjectTitle()+"</i>";
							deptId =  "<b>"+prefix+"</b>" + deptId.substring(prefix.length());
							String out = deptId +" " + title +"\n";
							if(!response.contains(out))
								response += out;
						}
					}
					if(diol.size() == 1)
						firstDept = diol.get(0);
				} 
			CourseInformationObject firstCourse = null;
				if (searchTerms.length == 2 || firstDept != null) {
					if(firstDept != null)
						dept = firstDept.getDepartmentId();
					else
						dept = searchTerms[0];
					if(searchTerms.length == 2)
						prefix = searchTerms[1];
					else
						prefix = "";
					List<CourseInformationObject> ciol = d.queryCourseByPrefix(dept, prefix);
					if(!ciol.isEmpty()){
						for(Iterator<CourseInformationObject> i = ciol.iterator(); i.hasNext();) {
							CourseInformationObject cio = i.next();
							String deptId = "<b>"+cio.getDepartmentId()+"</b>";
							String courseId = cio.getCourseId();
							String courseTitle ="<i>"+ cio.getCourseTitle()+"</i>";
							courseId =  "<b>"+prefix+"</b>" + courseId.substring(prefix.length());
							String out =  deptId +" " + courseId + " " +courseTitle +"\n";
							if(!response.contains(out))
								response += out;
						}
					}
					if(ciol.size() == 1)
						firstCourse = ciol.get(0);
				}
				if (searchTerms.length == 3|| firstCourse !=null) {
					dept = searchTerms[0];
					if(searchTerms.length >= 2)
						course = searchTerms[1];
					else if (firstCourse != null)
						course = firstCourse.getCourseId();
					if(searchTerms.length == 3)
						prefix = searchTerms[2];
					else
						prefix = "";
					List<SectionInformationObject> siol = d.querySectionByPrefix(dept, course, prefix);
					if(!siol.isEmpty()){
						for(Iterator<SectionInformationObject> i = siol.iterator(); i.hasNext();) {
							SectionInformationObject sio = i.next();
							String deptId = "<b>"+sio.getDepartmentId()+"</b>";
							String courseId = "<b>"+sio.getCourseId()+"</b>";
							String sectionId = sio.getSectionId();
							String activity ="<i>"+ sio.getActivity()+"</i>";
							sectionId = "<b>"+prefix+"</b>" +sectionId.substring(prefix.length());
							String out= deptId +" " + courseId +" "+ sectionId+ " "+activity+"\n";
							if(!response.contains(out))
								response += out;
						}
					}
				}

			}
		if(response.isEmpty())
				response = "<i>No suggestions found</i>\n";
		try {
			resp.setContentType("text/xml");
			resp.getWriter().write(response);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
